package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Comment;
import com.example.exception.FloraException;
import com.example.mapper.CommentMapper;
import com.example.service.CommentService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-11-21 19:18:49
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserService userService;


    /** 显示评论时先只展示根评论，若要查看某条根评论的子评论或子评论下的子评论，都要点击展开按钮
     *  type:0文章评论  1日记评论
     *  展示时按时间降序
     * @return*/
    @Override
    public IPage<Comment> getRootComment(int type,Long articleId, int pageNum, int pageSize) {
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        Page<Comment> page = new Page<>(pageNum,pageSize);
        qw.eq(Comment::getType,type);
        qw.eq(Comment::getArticleId,articleId);
        qw.eq(Comment::getParentId,-1);

        return page(page,qw);
    }

    /** 查询某条评论下的子评论 */
    @Override
    public IPage<Comment> getChildrenComment(Long commentId, int pageNum, int pageSize) {
         LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
         Page<Comment> page = new Page<>(pageNum,pageSize);
         qw.eq(Comment::getParentId,commentId);
         return page(page,qw);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Comment addComment(Comment comment) throws FloraException {
        try {
            this.baseMapper.insert(comment);
            return comment;
        } catch (Exception e) {
            throw new FloraException("添加评论失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteComment(Long commentId) throws FloraException {
        /** 先查看该评论是否存在 */
        Comment comment = this.baseMapper.selectById(commentId);
        if(comment==null){
            throw new FloraException("该评论不存在");
        }

        /** 先删除该评论对应的子评论，再删除父评论 */
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Comment::getParentId,commentId);
        try{
            this.baseMapper.delete(qw);
        } catch (Exception e) {
            throw new FloraException("删除该评论的子评论失败"+e.getMessage());
        }

        try{
            this.baseMapper.deleteById(commentId);
        } catch (Exception e) {
            throw new FloraException("删除该条评论失败");
        }
    }


//    @Override
//    public ResponseResult getCommentList(Long articleId, int pageNum, int pageSize) {
//        //根据文章id查询根评论
//        LambdaQueryWrapper<Comment> qw=new LambdaQueryWrapper<>();
//        qw.eq(Comment::getArticleId,articleId)
//          .eq(Comment::getRootId, SystemConstants.ROOT_COMMENT);//根评论
//
//        Page<Comment> page=new Page<>(pageNum,pageSize);
//        page(page,qw);
//
//        List<CommentVo> commentVoList= toCommentVoList(page.getRecords());
//
//        //查询子评论
//        for(CommentVo commentVo:commentVoList){
//            commentVo.setChildren(getChildren(commentVo.getId()));
//        }
//
//        return ResponseResult.okResult(new PageVo(commentVoList, (long) commentVoList.size()));
//    }
//
//    public List<CommentVo> toCommentVoList(List<Comment> commentList){
//        /**
//        List<CommentVo> commentVos=BeanCopyUtils.copyBeanList(commentList,CommentVo.class);
//        for(CommentVo commentVo:commentVos){
//            //查询发评论人昵称
//            User userPost=userService.getById(commentVo.getCreateBy());
//            commentVo.setUsername(userPost.getNickName());
//            // 回复给的用户昵称
//            if(commentVo.getToCommentId()!=-1) {
//                User toUser = userService.getById(commentVo.getToCommentId());
//                commentVo.setToCommentUserName(toUser.getNickName());
//            }
//        }
//        return commentVos;
//         */
//
//        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(commentList, CommentVo.class);
//        //遍历vo集合
//        for (CommentVo commentVo : commentVos) {
//            //通过creatyBy查询用户的昵称并赋值
//            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
//            commentVo.setUsername(nickName);
//            //通过toCommentUserId查询用户的昵称并赋值
//            //如果toCommentUserId不为-1才进行查询
//            if(commentVo.getToCommentUserId()!=-1){
//                String toCommentUserName = userService.getById(commentVo.getToCommentUserId()).getNickName();
//                commentVo.setToCommentUserName(toCommentUserName);
//            }
//        }
//        return commentVos;
//    }
//
//    public List<CommentVo> getChildren(Long commentId){
//        LambdaQueryWrapper<Comment> qw=new LambdaQueryWrapper<>();
//        qw.eq(Comment::getRootId,commentId)
//          .orderByDesc(Comment::getCreateTime);
//        List<Comment> comments=list(qw);
//        //把子评论转为vo
//        return toCommentVoList(comments);
//    }
}
